package com.xfhy.selectcity.select.viewmodel;

import android.text.TextUtils;

import com.ludashi.framework.ApplicationHolder;
import com.xfhy.selectcity.adapter.AreaItem;
import com.xfhy.selectcity.adapter.AreaTitle;
import com.xfhy.selectcity.adapter.BaseAreaType;
import com.xfhy.selectcity.data.Area;
import com.xfhy.selectcity.data.AreaRepository;

import java.util.ArrayList;
import java.util.List;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

/**
 * @author : xfhy
 * Create time : 2021/2/25 17:20
 * Description :
 */
public class SelectProvinceViewModel extends ViewModel {

    private final AreaRepository mAreaRepository;

    private final static String[] HOT_CITIES_NAME = new String[]{"定位", "北京", "上海", "广州", "深圳", "杭州", "成都", "重庆"};
    private final static String[] ALL_PROVINCE_NAME = new String[]{"北京", "上海", "天津", "重庆", "黑龙江", "吉林", "辽宁", "内蒙古", "河北", "山西", "陕西", "山东", "新疆", "西藏", "青海"
            , "甘肃", "宁夏", "河南", "江苏", "湖北", "浙江", "安徽", "福建", "江西", "湖南", "贵州", "四川", "广东", "云南", "广西", "海南", "香港", "澳门", "台湾"};
    private final static List<AreaItem> HOT_CITIES = new ArrayList<>(8);
    private final static List<AreaItem> ALL_PROVINCE = new ArrayList<>(34);

    static {
        for (String city : HOT_CITIES_NAME) {
            HOT_CITIES.add(new AreaItem(city));
        }
        for (String province : ALL_PROVINCE_NAME) {
            ALL_PROVINCE.add(new AreaItem(province));
        }
    }

    public SelectProvinceViewModel() {
        mAreaRepository = new AreaRepository(ApplicationHolder.get());
    }

    public static List<AreaItem> getHotCities() {
        return HOT_CITIES;
    }

    public static List<AreaItem> getAllProvince() {
        return ALL_PROVINCE;
    }

    public List<BaseAreaType> initHotData() {
        List<BaseAreaType> areaTypeList = new ArrayList<>();
        areaTypeList.add(new AreaTitle("热门城市"));
        areaTypeList.addAll(HOT_CITIES);
        areaTypeList.add(new AreaTitle("省份"));
        areaTypeList.addAll(ALL_PROVINCE);
        return areaTypeList;
    }

    public LiveData<List<Area>> searchAreasByKey(String key) {
        if(TextUtils.isEmpty(key)) {
            return new MutableLiveData<>();
        }
        return mAreaRepository.searchAreasByKey(key);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
    }

}

